package com.cai.oamanager.controller;

import com.cai.oamanager.entity.*;
import com.cai.oamanager.service.DepartService;
import com.cai.oamanager.service.PositionService;
import com.cai.oamanager.service.StudentService;
import com.cai.oamanager.service.UserService;
import com.cai.oamanager.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/16 22 : 08
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private DepartService departService;

    @Autowired
    private UserService userService;
    private final int size=5;

    /**
     *
     * @param request
     * @param model
     * @param sid
     * @param currentPage
     * @return
     */
    @RequestMapping("/student.html")
    public String allStudent(HttpServletRequest request, Model model,@RequestParam(defaultValue = "") Integer sid,@RequestParam(defaultValue = "1") Integer currentPage){
        People people = (People) request.getSession().getAttribute("people");
        int start = (currentPage-1)*size;

        List<Student> allStudent = studentService.getAllStudent(people,sid,start,size);
        int studentCount = studentService.getStudentCount(people);
        int totalPage = studentCount%size==0?studentCount/size:studentCount/size+1;
        model.addAttribute("studentList",allStudent);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("sid",sid);
        return "student";
    }
    @RequestMapping("toAddStudent")
    public String toAddStudent(Model model){
        List<Depart> allDepart = departService.getAllDepart(0, 5);
        List<Domain> allDomain = departService.getAllDomain();
        List<People> allFDY = userService.getAllFDY();
        model.addAttribute("departList",allDepart);
        model.addAttribute("fdyList",allFDY);
        model.addAttribute("domainList",allDomain);
        return "studentAdd";
    }
    @RequestMapping("addStudent")
    public String addStudent(Student student){
        student.setSid(Utils.getNumber());
        student.setSintime(Utils.getDate());
        studentService.addStudent(student);
        return "redirect:/student/student.html";
    }
    /**
     * 详情
     */
    @RequestMapping("detail/{sid}")
    public String detail(Model model,@PathVariable("sid") Integer sid){
        Student student = studentService.getStudentById(sid);
        model.addAttribute("student",student);
        return "studentDetail";
    }

    @RequestMapping("toEdit/{sid}")
    public String toEdit(Model model,@PathVariable("sid") Integer sid){
        Student student = studentService.getStudentById(sid);
        List<Depart> departList = studentService.getAllDepartByStudentIdOfDomain(sid);
        model.addAttribute("student",student);
        model.addAttribute("departList",departList);
        //        SELECT d.major from student s JOIN depart d on s.doid=d.domain AND s.sid=201926501
        return "studentEdit";
    }

    @RequestMapping("/update/updateStudent")
    public String updateStudent(Student student){
        System.out.println("student==>"+student);
        studentService.editStudent(student);
        return "redirect:/student/toEdit/"+student.getSid();
    }

    /**
     * ajax请求
     * @param departid
     * @return
     */
    @RequestMapping("ajax")
    public @ResponseBody List<People> getPeopleByDepart(@RequestParam("departid") Integer departid){
        Map<Object,Object> map = new HashMap<>();
        List<People> peopleByDepart = studentService.getPeopleByDepart(departid);
        System.out.println("departid::  "+departid);
        return peopleByDepart;
    }

}
